package me.shurufa.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.github.ybq.android.spinkit.SpinKitView;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.utils.Constants;
import me.shurufa.utils.StatusBarCompat;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Author : andy
 * Date   : 16/4/20 15:50
 * Email  : andyxialm@gmail.com
 * Github : github.com/andyxialm
 * Description : 看大图界面
 */
public class PhotoViewManager extends BaseActivity implements PhotoViewAttacher.OnPhotoTapListener {

    @Bind(R.id.iv_photo)
    PhotoView iv_photo;

    @Bind(R.id.progress)
    SpinKitView progress;

    private static final String IMAGE_URL = "IMAGE_URL";
    private Handler mHandler = new Handler();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_lookat_image;
        super.onCreate(savedInstanceState);

        String imgUrl = getIntent().getStringExtra(IMAGE_URL);
        if (TextUtils.isEmpty(imgUrl)) {
            return;
        }

        iv_photo.setOnPhotoTapListener(this);

        try {
            /*Glide.with(this)
                    .load(imgUrl).asBitmap()
                    .placeholder(R.drawable.ic_placeholder_digest_image)
                    .centerCrop()
                    .into(new BitmapImageViewTarget(iv_photo) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            iv_photo.setImageBitmap(resource);
                        }
                    });*/
            progress.setVisibility(View.VISIBLE);
            Glide.with(this)
                    .load(imgUrl)
                    .placeholder(R.drawable.ic_placeholder_digest_image)
                    .into(new SimpleTarget<GlideDrawable>() {
                        @Override
                        public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                            progress.setVisibility(View.GONE);
                            iv_photo.setImageDrawable(resource);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void loadImage(Context context, String url) {
        Intent in = new Intent(context, PhotoViewManager.class);
        in.putExtra(IMAGE_URL, url);
        context.startActivity(in);
    }

    @Override
    public void initStatusBar() {
        StatusBarCompat.setColor(this, getResources().getColor(R.color.black));
    }

    @Override
    public void onPhotoTap(View view, float v, float v1) {
        finish();
    }

    @Override
    public void onOutsidePhotoTap() {
        finish();
    }

}
